#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWDetailListEntry.h"
#import "framework/FWDetailListSection.h"

#import "PICommon.h"
#import "PITaskPlugin.h"
#import "PITaskStatus.h"
#import "PIContextList.h"
#import "PIIntegration.h"
#import "PITaskIntegration.h"
#import "PIContext.h"


@implementation PIContextList

@synthesize selected, earliest;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithPreferences:(FWPreferences*)prefs
{
    if ((self = [super init])) {
        if ([prefs boolValue:kPIOptionsHideFuture withDefault:kPIOptionsHideFutureDefault])
            self.earliest = FWLocalDateForLocalMidnight();
    }
    
    return self;
}

- (void)dealloc
{
    [selected release];
    [earliest release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Handling user input

- (BOOL)cancelClicked:(FWApplication*)application sections:(NSArray*)sections
{
    return YES;
}

- (BOOL)selectClicked:(FWApplication*)application sections:(NSArray*)sections
{
    [application setAttribute:kPIApplicationContextLocation value:self.selected];
  
    PITaskStatus* status = (PITaskStatus*)application.info;
    status.autoContextOn = YES;
    
    // Turn off fixed context mode if not combining contexts
    status.fixedContextOn = status.fixedContextOn && combineMode;
    
    PITaskPlugin* plugin = (PITaskPlugin*)application.fwPlugin;
    [plugin hideToolbar];
    [plugin updateStatusbar];
    [plugin triggerUnconditionalRefresh];
    
    return YES;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Accessing records from the database

- (NSArray*)dataForDetailListPage:(NSObject *)page key:(NSObject*)key application:(FWApplication *)application
{  
    self.selected = [PIIntegration locationContextsFromPI];
    
    NSArray* contexts = [PITaskIntegration arrayOfContexts:application earliest:earliest counts:YES];
    combineMode = [FWPreferences boolValue:kPITaskOptionsCombineContexts
                               withDefault:kPITaskOptionsCombineContextsDefault
                               preferences:application.fwPlugin.liPlugin.preferences];
    
    NSMutableArray* contents = [NSMutableArray arrayWithCapacity:contexts.count];
    for (PIContext* context in contexts) {
        FWDetailListEntry* entry = [[FWDetailListEntry entryWithTitle:context.title
                                                                value:[NSNumber numberWithBool:
                                                                       [self.selected piContextForKey:context.key] != nil]]
                                    withKey:[NSNumber numberWithInt:context.key]];
        entry.selected = YES;
        entry.info = [[NSNumber numberWithInt:context.count] stringValue];
        
        if (!context.isLocationAware)
            entry.color = [UIColor lightGrayColor];
        
        [contents addObject:entry];
    }
    
    return [NSArray arrayWithObject:[FWDetailListSection sectionForElements:contents]];
}

@end
